package com.chat.datea.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chat.datea.R;
import com.chat.datea.adapter.AlbumFragment_RecyclerView_Adapter;
import com.chat.datea.base.BaseFragment;
import com.chat.datea.base.Finnal_Base;
import com.chat.datea.bean.AlbumList;
import com.chat.datea.utils.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xyb on 15/10/23.
 */
public class AlbumListFragment extends BaseFragment {
    private AlbumFragment_RecyclerView_Adapter recyclerViewAdapter;
    private String albumListUrl;
    private RecyclerView recyclerView;
    private VolleySingleton singleton;
    private List<AlbumList.AlbumlistEntity> albumlist;
    private String Albums;

    public AlbumListFragment(String ting_uid,String Albums) {
        this.Albums = Albums;
        singleton = VolleySingleton.getVolleySingleton(mActivity);
        albumListUrl = Finnal_Base.apiUrl + Finnal_Base.method + "baidu.ting.artist.getAlbumList" + Finnal_Base.format
                + Finnal_Base.order + "1" + Finnal_Base.tinguid + ting_uid + Finnal_Base.offset + "0" + Finnal_Base.limits + "30";
        Log.e("AlbumListFragment", albumListUrl);
    }

    @Override
    protected void initData() {

        StringRequest request = new StringRequest(albumListUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("AlbumListFragment", response);
                Gson gson = new Gson();
                if (!Albums.equals("0")){
                    AlbumList albumList = gson.fromJson(response, AlbumList.class);
                    albumlist = new ArrayList<>();
                    albumlist = albumList.getAlbumlist();
                    recyclerViewAdapter = new AlbumFragment_RecyclerView_Adapter(mActivity, albumlist,getActivity().getSupportFragmentManager());
                    recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
                    recyclerView.setAdapter(recyclerViewAdapter);
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        singleton.addQueue(request, "AlbumListFragment");

    }

    @Override
    protected void initView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.singer_listView);
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.singer_recycview, null);
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        singleton.removeRequest("AlbumListFragment");
    }
}
